// @algorithm @lc id=581 lang=cpp 
// @title shortest-unsorted-continuous-subarray


#define print(...)
// @test([2,6,4,8,10,9,15])=5
// @test([1,2,3,4])=0
// @test([1])=0
class Solution {
public:
    // nums: [A] <= [B] <=[C]
    int findUnsortedSubarray(vector<int>& nums) {
        const int n = (int)nums.size();
        int left=n,   lower=INT_MAX, // left 记录A)的右开界
            right=-1, upper=INT_MIN; // right记录(C的左开界
        for(int i=0; i<(int)nums.size(); i++){
            lower<nums[n-1-i] ? left=n-1-i : lower = nums[n-1-i];
            nums[i] < upper   ? right=i    : upper = nums[i];
        }
        print("left=", left, ", right=", right, "\n")
        return right<left ? 0 : right-left+1;
    }
};